/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.asn1.lang;

import java.io.UnsupportedEncodingException;
 

/**
 * ASN.1 data type class for the ASN.1 OctetString type.
 *
 * @author Thomas Kistel
 * @version $Revision: 763 $
 *
 */
public class OctetString {

    private String string;
    
    private int size = -1;
    
    public OctetString() {
        
    }
    
    /**
     * Creates an OctetString with the given size constraint.
     * 
     * @param size the size constraint
     */
    public OctetString(int size) {
        this.size = size;
    }
    
    /**
     * Creates an OctetString with the given string.
     * 
     * @param string the String
     */
    public OctetString(String string) {
        this.string = string;
    }
    
    /**
     * Creates an OctetString with the given size constraint and the string.
     * 
     * @param size the size constraint
     * @param string the String
     * @throws UnsupportedEncodingException if the length of the string is greater than the size
     */
    public OctetString(int size, String string) throws UnsupportedEncodingException {
        this(size);
        int length = string.length();
        if(size > 0 && size < length) {
            throw new UnsupportedEncodingException("The size (" + length + ") of the given OctetString is greather than the allowed size (" + size + ")");
        }
        this.string = string;
    }
    
    @Override
    public String toString() {
        return string;
    }
    
    /**
     * Returns the allowed size of the OctetString. If the String exceed this size and {@link UnsupportedEncodingException}
     * should be thrown by this class.
     * 
     * @return the allowed size of the OctetString
     */
    public int getSize() {
        return size;
    }
}
